(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('usersCtrl',usersCtrl);

    function usersCtrl(authorizationManager, usersManager, ModalService, Constants){

        this.users = usersManager.getUsers();

        this.getRole = function(user){
            switch(user.role){
                case "0": return "Admin";
                case "1": return "Manager";
                case "2": return "Salesman";
            }
        };

        this.editUser = function(user){
            ModalService.showModal({
                templateUrl: 'editUser',
                controller: 'editUserController as euc',
                inputs:{
                    user: user
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.UPDATE){
                            usersManager.updateUser(result.user);
                        }else if(result.action === Constants.COMMANDS.DELETE){
                            usersManager.deleteUser(result.user);
                        }
                    });
                });
        };
    };

    angular.module('ytong.controllers').controller('editUserController', editUserController);

    function editUserController(close, user){

        this.user = angular.copy(user);

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.user = this.user;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };

}(angular));